package com.apes.fn.scm.internal.help.service.voucher.inner;

import com.apes.fn.scm.internal.help.model.InternalHelpReturnApproval;
import com.apes.fn.scm.internal.help.model.InternalHelpReturnApprovalItem;
import com.apes.fn.scm.internal.help.repository.InternalHelpReturnApprovalRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.util.MapUtil;
import com.apes.scm.account.account.model.InventoryBatch;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.voucher.model.Voucher;
import com.apes.scm.voucher.model.VoucherItem;
import com.apes.scm.voucher.service.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

/**
 * 门店内协退回凭证
 *
 * @Author wuq
 * @Date 2021-11-5
 */
@Service
public class StoreInnerHelpReturnVoucherService extends DomainService {

    @Autowired
    private InternalHelpReturnApprovalRepository internalHelpReturnApprovalRepository;

    /**
     * 生成门店经销部分的返厂凭证
     *
     * @param internalHelpReturnApproval 内协退货单
     */
    public void process(InternalHelpReturnApproval internalHelpReturnApproval) {
        Voucher voucher = new Voucher();
        voucher.setReturned(true);
        voucher.setBusinessType(BusinessType.PURCHASE_RETURN);

        PartyRole supplier = internalHelpReturnApproval.getReceiptLocation().getCompany().getParty().getPatyRoleByType("Supplier");
        voucher.setCompany(internalHelpReturnApproval.getCompany());
        voucher.setPartner(supplier);

        InternalHelpReturnApprovalItem internalHelpReturnApprovalItem = internalHelpReturnApproval.getApprovalItems().iterator().next();
        InventoryBatch inventoryBatch = internalHelpReturnApprovalItem.getInventoryBatch();
        voucher.setDept(inventoryBatch.getDept());

        voucher.setInventoryMode(inventoryBatch.getCooperation().getInventoryMode());
        voucher.setAccountingMethod(inventoryBatch.getCooperation().getAccountingMethod());
        voucher.setContract(inventoryBatch.getContract());

        voucher.setScene(internalHelpReturnApproval.getScene());
        voucher.setOrigin(internalHelpReturnApproval.getId());
        voucher.setOriginBusinessType(internalHelpReturnApproval.getInnerBusinessType());
        voucher.setSource(internalHelpReturnApproval.getId());
        voucher.setSourceBusinessType(internalHelpReturnApproval.getInnerBusinessType());
        voucher.setClerk(internalHelpReturnApproval.getApplyUid());
        voucher.setCustomerExpand(internalHelpReturnApproval.getCustomerExpand().getId());

        voucher.setVoucherItems(
                internalHelpReturnApproval.getApprovalItems().stream().map(
                        item -> {
                            VoucherItem voucherItem = new VoucherItem();
                            voucherItem.setProduct(item.getProduct());

                            voucherItem.setOwner(internalHelpReturnApproval.getShipLocation().getCompany());
                            voucherItem.setUom(item.getUom());
                            voucherItem.setProductQty(-1.00 * item.getProductReturnQty());
                            voucherItem.setPriceUnit(item.getPriceUnit());

                            voucherItem.setInventoryBatch(item.getInventoryBatch());
                            voucherItem.setTax(item.getInventoryBatch().getTax());

                            voucherItem.setOriginItemId(String.valueOf(item.getId()));
                            voucherItem.setSourceItemId(String.valueOf(item.getId()));
                            return voucherItem;
                        }).collect(Collectors.toSet()));

        Voucher newVoucher = invoke("voucher.create", MapUtil.mapped(voucher));
        invoke("invoice.create", MapUtil.mapped(invoke("voucher.flow.invoice", MapUtil.mapped(newVoucher))));

        // 回写內协退货单状态
        InternalHelpReturnApproval approval = internalHelpReturnApprovalRepository.findOne(internalHelpReturnApproval.getId());
        if ("receipt".equals(approval.getState())) {
            approval.setState("done");
            internalHelpReturnApprovalRepository.saveAndFlush(approval);
        }
    }

}
